package edu.sysu.cshare.util.gui;

import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;

import edu.sysu.cshare.control.MessageManager;
import edu.sysu.cshare.gui.Pnl_Message;
import edu.sysu.cshare.model.Message;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.model.Message.Category;

public class TableModel_Msg extends AbstractTableModel {
	Category category;
	private  final String[] columnNames = { "id", "下载附件", "标题", "评论" };
	MessageManager messageManager;
	List<Message> messages;
	User user;

	public TableModel_Msg(Category category, MessageManager messageManager) {
		super();
		this.category = category;
		this.messageManager = messageManager;
		this.user = messageManager.getActor();
		this.messages = messageManager.getByCategory(category);
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (columnIndex == 1) {
			return ImageIcon.class;
		} else {
			return getValueAt(0, columnIndex).getClass();
		}
	}

	@Override
	public int getColumnCount() {

		return columnNames.length;
	}

	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}

	@Override
	public int getRowCount() {

		return messages.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {

		Object value = null;
		Message msg = messages.get(rowIndex);
		switch (columnIndex) {
		case 0:
			value = msg.getId();
			break;
		case 1:
			if (msg.hasAttach()) {
				value = Pnl_Message.Image.getDownload();
			}
			break;
		case 2:
			value = msg.getTitle();
			break;
		case 3:
			value = msg.getComments().size();
			break;
		}
		return value;
	}

}
